#include <fstream>
#include <iostream>
#include <string>

#include "contacts.pb.h"

void appendPeopleInfo(contacts::PepoleInfo* people_info) {
    std::cout << "-------------新增联系人-------------" << std::endl;

    std::cout << "please input name: ";
    std::string name;
    std::getline(std::cin, name);
    people_info->set_name(name);

    std::cout << "please input age: ";
    int age = 0;
    std::cin >> age;
    std::cin.ignore(256, '\n');
    people_info->set_age(age);

    std::cout << "please input phone number(input Enter only to stop)"
              << std::endl;
    std::string phone;
    while (true) {
        std::cout << "phone number: ";
        std::getline(std::cin, phone);
        if (phone.empty()) {
            break;
        }

        contacts::Phone* c_phone = people_info->add_phones();
        c_phone->set_number(phone);

        std::cout << "please input phone number type(1 - MP, 2 - TEL): ";
        int type = 0;
        std::cin >> type;
        std::cin.ignore(256, '\n');
        switch (type) {
            case 1:
                c_phone->set_phone_type(
                    contacts::Phone::PhoneType::Phone_PhoneType_MP);
                break;
            case 2:
                c_phone->set_phone_type(
                    contacts::Phone::PhoneType::Phone_PhoneType_TEL);
                break;
            default:
                break;
        }
    }

    contacts::Address address;
    std::cout << "please input home address: ";
    std::string home_address;
    getline(std::cin, home_address);
    address.set_home_address(home_address);

    std::cout << "please input util address: ";
    std::string util_address;
    getline(std::cin, util_address);
    address.set_unit_address(util_address);

    people_info->mutable_any()->PackFrom(address);

    std::cout << "please input contact method(1-qq    2-wechat): ";
    int way = 0;
    std::cin >> way;
    std::cin.ignore(256, '\n');
    switch (way) {
        case 1: {
            std::string qq;
            std::cout << "QQ: ";
            std::getline(std::cin, qq);
            people_info->set_qq(qq);
            break;
        }
        case 2: {
            std::string wechat;
            std::cout << "WeChat: ";
            std::getline(std::cin, wechat);
            people_info->set_wechat(wechat);
            break;
        }
        default: {
            std::cout << "input error" << std::endl;
            break;
        }
    }

    std::cout << "please input note(input Enter only to finish): " << std::endl;
    for (int i = 0;; ++i) {
        std::cout << "input title: ";
        std::string title;
        std::getline(std::cin, title);
        if (title.empty()) {
            break;
        }
        title = "(" + std::to_string(i + 1) + ")" + title;

        std::cout << "input content: ";
        std::string content;
        std::getline(std::cin, content);

        people_info->mutable_note()->insert({title, content});
    }

    std::cout << "-----------添加联系人成功-----------" << std::endl;
}

int main() {
    contacts::Contacts contacts;

    // 1. 打开本地通讯录，没有就创建
    std::fstream input("contacts.txt", std::ios::in | std::ios::binary);
    if (input.is_open() == false) {
        std::cout << "file not find, create a new file" << std::endl;
    } else if (contacts.ParseFromIstream(&input) == false) {
        std::cout << "parse failed" << std::endl;

        input.close();
        return -1;
    }

    // 2. 向通讯录添加一个联系人
    appendPeopleInfo(contacts.add_people_info());

    // 3. 将通讯录序列化后写入文件
    std::fstream output("contacts.txt",
                        std::ios::out | std::ios::trunc | std::ios::binary);
    if (output.is_open() == false) {
        std::cout << "file not find" << std::endl;
        input.close();
        return -1;
    } else if (contacts.SerializeToOstream(&output) == false) {
        std::cout << "serialize failed" << std::endl;

        input.close();
        output.close();
        return -1;
    }

    input.close();
    output.close();

    return 0;
}
